package cn.cable.monitor.sdk.push.impl;

import cn.cable.monitor.sdk.model.LogMessage;
import cn.cable.monitor.sdk.push.IPush;
import com.alibaba.fastjson.JSON;
import org.redisson.Redisson;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * redis 发布订阅方式进行推送消息
 *
 * @author hsj
 * @date 2024/7/16 16:44
 */
public class RedisPush implements IPush {

	private final Logger logger = LoggerFactory.getLogger(RedisPush.class);


	private RedissonClient redissonClient;

	@Override
	public void open(String host, int port, String password) {
		if (null != redissonClient && redissonClient.isShutdown()) {
			return;
		}
		Config config = new Config();
		config.setCodec(JsonJacksonCodec.INSTANCE);
		config.useSingleServer()
		      .setAddress("redis://" + host + ":" + port)
		      .setConnectionPoolSize(64)
			.setPassword(password)
		      .setDatabase(6)
		      .setConnectionMinimumIdleSize(10)
		      .setIdleConnectionTimeout(1000)
		      .setConnectTimeout(1000)
		      .setRetryAttempts(3)
		      .setRetryInterval(1000)
		      .setPingConnectionInterval(0)
		      .setKeepAlive(true);
		redissonClient = Redisson.create(config);
		RTopic topic = this.redissonClient.getTopic("business-behavior-monitor-sdk-topic");
		topic.addListener(LogMessage.class, new Listener());
	}

	class Listener implements MessageListener<LogMessage> {

		@Override
		public void onMessage(CharSequence charSequence, LogMessage logMessage) {
			System.out.println("接收到的消息:[" + JSON.toJSONString(logMessage) + "]");
		}

	}

	@Override
	public void send(LogMessage logMessage) {
		try {
			RTopic topic = redissonClient.getTopic("business-behavior-monitor-sdk-topic");
			topic.publish(logMessage);
		} catch (Exception e) {
			logger.error("警告: 业务行为监控组件，推送日志消息失败", e);
		}

	}

}
